/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.gen.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thinkgem.jeesite.common.persistence.BasePage;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.gen.entity.GenTable;
import com.thinkgem.jeesite.modules.gen.service.GenTableService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 代码生成-表信息Controller
 *
 * @author kongxiangsheng
 * @version 2018-08-16
 */
@Controller
@RequestMapping(value = "${adminPath}/gen/genTable")
public class GenTableController extends BaseController {
    @Autowired
    private GenTableService genTableService;

    @RequiresPermissions("gen:genTable:view")
    @RequestMapping(value = {"list", ""})
    public String list(GenTable genTable, HttpServletRequest request, HttpServletResponse response, Model model) {
        BasePage<GenTable> page = new BasePage<GenTable>(request, response);
        QueryWrapper queryWrapper = new QueryWrapper<GenTable>(genTable);
        queryWrapper.orderByAsc(GenTable.COL_NAME);
        genTableService.page(page, queryWrapper);

        model.addAttribute("page", page);
        return "modules/gen/GenTable/list";
    }

    @RequiresPermissions("gen:genTable:view")
    @RequestMapping(value = "form")
    public String form(GenTable genTable, Model model) {
        // 点击“业务表添加”：只需要获取物理表列表即可
        if (StringUtils.isBlank(genTable.getId()) && StringUtils.isBlank(genTable.getName())) {
            List<GenTable> sourceTableList = genTableService.findTableListFormDb(new GenTable());
            model.addAttribute("sourceTableList", sourceTableList);
        } else if (StringUtils.isBlank(genTable.getId()) && StringUtils.isNotBlank(genTable.getName())) {
            // 点击“下一步”：需要获取物理表详细信息
            if (!genTableService.checkTableName(genTable.getName())) {
                addMessage(model, "下一步失败！" + genTable.getName() + " 表已经添加！");
                genTable.setName("");
            } else {
                genTable = genTableService.getTableFormDb(genTable);
            }
        } else {
            // 修改：直接查询即可
            genTable = genTableService.getWithColumns(genTable.getId());
        }

        // 其他情况均需获取已经添加完成表列表
        List<GenTable> targetTableList = genTableService.getAllTableDict();

        model.addAttribute("targetTableList", targetTableList);
        model.addAttribute("genTable", genTable);

        return "modules/gen/GenTable/form";
    }

    @RequiresPermissions("gen:genTable:edit")
    @RequestMapping(value = "save")
    public String save(GenTable genTable, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, genTable)) {
            return form(genTable, model);
        }
        // 验证表是否已经存在
        if (StringUtils.isBlank(genTable.getId()) && !genTableService.checkTableName(genTable.getName())) {
            addMessage(model, "保存失败！" + genTable.getName() + " 表已经存在！");
            genTable.setName("");
            return form(genTable, model);
        }
        genTableService.saveWithChildren(genTable);
        addMessage(redirectAttributes, "保存业务表成功");
        return "redirect:" + adminPath + "/gen/genTable/?repage";
    }

    @RequiresPermissions("gen:genTable:edit")
    @RequestMapping(value = "delete")
    public String delete(GenTable genTable, RedirectAttributes redirectAttributes) {
        genTableService.deleteWithChildren(genTable);
        addMessage(redirectAttributes, "删除业务表成功");
        return "redirect:" + adminPath + "/gen/genTable/?repage";
    }

}